package cn.css0209.applicationdemo190509;

import android.app.Activity;
import android.content.DialogInterface;
import android.os.Bundle;
import android.support.v7.app.AlertDialog;
import android.support.v7.app.AppCompatActivity;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.EditText;
import android.widget.RelativeLayout;
import android.widget.Toast;

import cn.css0209.applicationdemo190509.utils.StatusBarUtil;

public class SettingActivity extends AppCompatActivity {
    private RelativeLayout change_pwd, about_us, agreement, version, common_problem, ip_modif;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_setting);
        topBar();
        init();
        OnClick RelativeonClick = new OnClick(this);
        change_pwd.setOnClickListener(RelativeonClick);
        about_us.setOnClickListener(RelativeonClick);
        agreement.setOnClickListener(RelativeonClick);
        version.setOnClickListener(RelativeonClick);
        common_problem.setOnClickListener(RelativeonClick);
        ip_modif.setOnClickListener(RelativeonClick);
    }

    public void init() {
        change_pwd = findViewById(R.id.change_pwd);
        about_us = findViewById(R.id.about_us);
        agreement = findViewById(R.id.agreement);
        version = findViewById(R.id.version);
        common_problem = findViewById(R.id.common_problem);
        ip_modif = findViewById(R.id.ip_modif);
    }

    public void back(View view) {
        this.finish();
    }


    public void topBar() {
        //当FitsSystemWindows设置 true 时，会在屏幕最上方预留出状态栏高度的 padding
        StatusBarUtil.setRootViewFitsSystemWindows(this, true);
        //设置状态栏透明
//        StatusBarUtil.setTranslucentStatus(this);
        //一般的手机的状态栏文字和图标都是白色的, 可如果你的应用也是纯白色的, 或导致状态栏文字看不清
        //所以如果你是这种情况,请使用以下代码, 设置状态使用深色文字图标风格, 否则你可以选择性注释掉这个if内容
        if (!StatusBarUtil.setStatusBarDarkTheme(this, false)) {
            //如果不支持设置深色风格 为了兼容总不能让状态栏白白的看不清, 于是设置一个状态栏颜色为半透明,
            //这样半透明+白=灰, 状态栏的文字能看得清
            StatusBarUtil.setStatusBarColor(this, 0x55000000);
        }
    }
}

class OnClick implements View.OnClickListener {
    final Activity activity;

    public OnClick(Activity activity) {
        this.activity = activity;
    }

    @Override
    public void onClick(View v) {
        int id = v.getId();
        switch (id) {
            case R.id.change_pwd:
                final EditText edit = new EditText(activity);
                AlertDialog.Builder editDialog = new AlertDialog.Builder(activity);
                editDialog.setTitle("修改密码");
                //设置dialog布局
                editDialog.setView(edit);

                //设置按钮
                editDialog.setPositiveButton(R.string.enter, new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int which) {
                        String input = edit.getText().toString();
                        String ts;
                        if (input.equals("")) {
                            ts = "密码不能为空";
                        } else if (input.length() < 6 || input.length() > 20) {
                            ts = "密码长度应在6-20字符";
                        } else {
                            ts = "修改成功！";
                        }
                        Toast.makeText(activity, ts, Toast.LENGTH_SHORT).show();
                    }
                }).setNegativeButton("取消", null);
                editDialog.create().show();
                break;
            case R.id.about_us:
                new AlertDialog.Builder(activity)
                        .setTitle("About Us")
                        .setMessage("Blog: https://css0209.cn\nGitHub: https://github.com/blankyk")
                        .setPositiveButton("确认", new DialogInterface.OnClickListener() {
                            @Override
                            public void onClick(DialogInterface dialog, int which) {
                                dialog.dismiss();
                            }
                        }).create().show();
                break;
            case R.id.agreement:
                final String radioItems[] = {"G17计应3", "G17计应1", "G17计应2"};
                new AlertDialog.Builder(activity)
                        .setTitle("Agreement")
                        .setSingleChoiceItems(radioItems, 0, new DialogInterface.OnClickListener() {
                            @Override
                            public void onClick(DialogInterface dialog, int which) {
                                Toast.makeText(activity, radioItems[which], Toast.LENGTH_SHORT).show();
                            }
                        }).setPositiveButton("确定", new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int which) {
                        dialog.dismiss();
                    }
                }).create().show();
                break;
            case R.id.version:
                final String checkBoxItems[] = {"Java", "Python", "PHP", "Go", "Web", "React", "SpringBoot"};
                final boolean check[] = {true,true,true,false,true,false,false};
                new AlertDialog.Builder(activity)
                        .setTitle("Version Information")
                        .setMultiChoiceItems(checkBoxItems, check, new DialogInterface.OnMultiChoiceClickListener() {
                            @Override
                            public void onClick(DialogInterface dialog, int which, boolean isChecked) {
                                String out;
                                if(isChecked){
                                    out = checkBoxItems[which]+" 选择";
                                }else{
                                    out = checkBoxItems[which]+" 未选择";
                                }
                                Toast.makeText(activity,out,Toast.LENGTH_SHORT).show();
                            }
                        }).setPositiveButton("Ok", new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int which) {
                        dialog.dismiss();
                    }
                }).create().show();
                break;
            case R.id.common_problem:
//                new ProgressDialog()  api26已启用
//                new ProgressBar(activity).;
                new AlertDialog.Builder(activity)
                        .setTitle("ProgressDialog 已被弃用")
                        .setMessage("ProgressDialog 在api 26已被弃用！")
                        .setPositiveButton("Ok", new DialogInterface.OnClickListener() {
                            @Override
                            public void onClick(DialogInterface dialog, int which) {
                                dialog.dismiss();
                            }
                        }).create().show();
                break;

            case R.id.ip_modif:
                final View myView = LayoutInflater.from(activity).inflate(R.layout.ip_layout, null);
                new AlertDialog.Builder(activity)
                        .setTitle("IP Modification")
                        .setView(myView)
                        .setPositiveButton("Ok", new DialogInterface.OnClickListener() {
                            @Override
                            public void onClick(DialogInterface dialog, int which) {
                                dialog.dismiss();
                            }
                        }).create().show();
                break;
        }
    }
}
